using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using TeamCommunitySolution.Domain.DataLayer.Base;
using TeamCommunitySolution.Helpers;
using TeamCommunitySolution.Models;
using TeamCommunitySolution.Services.Managers;
using TeamCommunitySolution.Services.Managers.Impl;
using TeamCommunitySolution.Services.Services;

namespace TeamCommunitySolution.Controllers
{

    /// <summary>
    /// Controller for the communication center.
    /// </summary>
    [Authorize]
    public class CommunicationCenterController : MasterController
    {

        /// <summary>
        /// the system user manager.
        /// </summary>
        private IApplicationService _service;

        /// <summary>
        /// Initializes a new instance of the <see cref="CommunicationCenterController"/> class.
        /// </summary>
        public CommunicationCenterController()
          : this(MvcApplication.ApplicationService)
        {
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="CommunicationCenterController"/> class.
        /// </summary>
        /// <param name="manager">The manager.</param>
        /// <param name="teamManager">The team manager.</param>
        private CommunicationCenterController(IApplicationService service)
        {
            this._service = service;
        }

        /// <summary>
        /// Indexes this instance.
        /// </summary>
        /// <returns>Action Result.</returns>
        public ActionResult Index()
        {
          SystemUser user = this._service.UserGetByUsername(User.Identity.Name);

            return View("Index", user);
        }

        /// <summary>
        /// Messages the inbox details.
        /// </summary>
        /// <param name="id">The id of the message inbox.</param>
        /// <returns>Action result.</returns>
        public ActionResult MessageInboxDetails(string id)
        {
          MessageInbox messageInbox = this._service.MessageGetReceivedForDisplay(id, this._service.UserGetByUsername(User.Identity.Name));

            return View("MessageInboxDetails", messageInbox);
        }

        /// <summary>
        /// Messages the inbox delete.
        /// </summary>
        /// <param name="messageId">The message id.</param>
        /// <returns></returns>
        public ActionResult MessageInboxDelete(string messageId) {
          SystemUser user = this._service.UserGetByUsername(User.Identity.Name);
          this._service.MessageDeleteInbox(messageId);
          return View("OperationCompleted");
        }

        /// <summary>
        /// Messages the outbox delete.
        /// </summary>
        /// <param name="messageId">The message id.</param>
        /// <returns></returns>
        public ActionResult MessageOutboxDelete(string messageId) {
          SystemUser user = this._service.UserGetByUsername(User.Identity.Name);
          this._service.MessageDeleteOutbox(messageId);
          return View("OperationCompleted");
        }

        /// <summary>
        /// Messages the outbox details.
        /// </summary>
        /// <param name="id">The id of the message outbox.</param>
        /// <returns>Action Result</returns>
        public ActionResult MessageOutboxDetails(string id)
        {
          MessageOutbox messageOutbox = this._service.MessageGetSentForDisplay(id, this._service.UserGetByUsername(User.Identity.Name));

            return View("MessageOutboxDetails", messageOutbox);
        }

        /// <summary>
        /// Creates the personal message.
        /// </summary>
        /// <returns>Action Result.</returns>
        public ActionResult CreatePersonalMessage()
        {
            return View();
        }

        /// <summary>
        /// Creates the personal message.
        /// </summary>
        /// <param name="ToUser">To user specified.</param>
        /// <param name="Title">The title of the message.</param>
        /// <param name="MessageContent">Content of the message.</param>
        /// <returns>Action Result.</returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreatePersonalMessage(string ToUser, string Title, string MessageContent)
        {
          SystemUser userTo = this._service.UserFindByFormattedName(ToUser);
          SystemUser userFrom = this._service.UserGetByUsername(User.Identity.Name);
          this._service.MessageSend(userTo.Id, userFrom.Id, Title, MessageContent);

            return View("OperationCompleted");
        }

        /// <summary>
        /// Creates the team message.
        /// </summary> 
        /// <returns>Action Result.</returns>
        public ActionResult CreateTeamMessage(string teamId)
        {
          Team team = this._service.TeamGetById(teamId);
            TeamContainer teamContainer = new TeamContainer { TeamFor = team };

            return View("CreateTeamMessage", teamContainer);
        }

        /// <summary>
        /// Creates the team message.
        /// </summary>
        /// <param name="title">The title.</param>
        /// <param name="messageContent">The message body.</param>
        /// <param name="teamId">The team id.</param>
        /// <returns>Action Result.</returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateTeamMessage(string title, string messageContent, string teamId)
        {
          SystemUser userFrom = this._service.UserGetByUsername(User.Identity.Name);
          this._service.MessageSendTeam(teamId, userFrom.Id, title, messageContent);

            return View("OperationCompleted");
        }
    }
}
